/*
 * Copyright (C) 2005-2006 Dmitri Maximovich <maxim@md.pp.ru>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
var P = {
  pref: function() {
	var prefs = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPrefService);
	return prefs.getBranch("cc.");
  },
  
  getString: function(name, deflt) {
  	try {
  	  return P.pref().getCharPref(name);
  	}
  	catch(e) {
  	  return deflt;
  	}
  },
  
  setString: function(name, value) {
  	P.pref().setCharPref(name, value);
  },
  
  delProp: function(name) {
    P.pref().deleteBranch(name);
  },
  
  getServerProp: function(id, name) {
	  return P.getString("server."+id+"."+name);
  },
  
  setServerProp: function(id, name, value) {
  	P.setString("server."+id+"."+name, value);
  },
  
  addServer: function(name, url, type) {
	var servers = P.getServers();
    var newId = P.nextId(servers);
    servers = (servers.length == 0 ? newId : servers+","+newId);

	P.setString("servers", servers);
	P.setServerProp(newId, "name", name);
	P.setServerProp(newId, "url", url);
	P.setServerProp(newId, "type", type);

	return newId; 
  },

  updateServer: function(id, name, url, type) {
	P.setServerProp(id, "name", name);
	P.setServerProp(id, "url", url);
	P.setServerProp(id, "type", type);
  },
  
  deleteServer: function(id) {
    // delete from servers variable
    var servers = P.getServersAsArray();
    var newServers = "";
    for(var i = 0; i < servers.length; i++) {
      var curr = parseInt(servers[i].id);
      if (curr != id) {
		newServers = newServers == "" ? curr : newServers+","+curr;
      }
    }
    P.setString("servers", newServers);
	// delete server properties
    P.delProp("server."+id);
  },
  
  // returns string of server ids
  getServers: function() {
    return P.getString("servers", "");
  },

  // returns array of server objects
  getServersAsArray: function() {
    var s = P.getServers();
    if (s.length == 0) {
      return new Array();
    }
    var ids = s.split(",");
    var result = new Array();
    for (var i = 0; i < ids.length; i++) {
      result.push(P.getServer(ids[i]));
    }
    return result;
  },
  
  // get server object by id
  getServer: function(id) {
    var server = new Object();
    server.id = id;
    server.name = P.getServerProp(server.id, "name");
    server.url = P.getServerProp(server.id, "url");
    server.type = P.getServerProp(server.id, "type");
    return server;
  },

  nextId: function(str) {
    if (str == null || str.length == 0) {
      return 1;
    }
    var s = str.split(",");
    var result = 0;
    for(i = 0; i < s.length; i++) {
      var ss = parseInt(s[i]);
      if (ss > result) {
        result = ss;
      }
    }
    return result + 1;    
  }
  
}
